package com.r2coding.music.controller;

import java.util.List;

import com.r2coding.common.enums.Module;
import com.r2coding.common.enums.OperationType;
import com.r2coding.music.annotation.SysLog;
import com.r2coding.music.vo.req.HandleSongListReqVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.r2coding.music.entity.SongEntity;
import com.r2coding.music.service.SongService;
import com.r2coding.common.util.Result;
import org.springframework.web.multipart.MultipartFile;

/**
 * 歌曲
 *
 * @author 98k灬
 * @email hk666zx@163.com
 * @date 2022-05-04 20:46:11
 */
@RestController
@RequestMapping("/song")
public class SongController {
    @Autowired
    private SongService songService;

    /**
     * 保存
     */
    @SysLog(module = Module.MUSIC, operationType = OperationType.INSERT, description = "保存歌曲")
    @PreAuthorize("hasAuthority('music:add')")
    @PostMapping("/save")
    public Result save(@RequestBody SongEntity song){
		songService.saveSong(song);
        return Result.ok();
    }

    /**
     * 修改
     */
    @SysLog(module = Module.MUSIC, operationType = OperationType.UPDATE, description = "修改歌曲")
    @PreAuthorize("hasAuthority('music:edit')")
    @PutMapping("/update")
    public Result update(@RequestBody SongEntity song){
		songService.updateById(song);
        return Result.ok();
    }

    /**
     * 删除
     */
    @SysLog(module = Module.MUSIC, operationType = OperationType.DELETE, description = "删除歌曲")
    @PreAuthorize("hasAuthority('music:delete')")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
		songService.deleteSongById(id);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PreAuthorize("hasAuthority('music:edit')")
    @PutMapping("/updateRelease")
    public Result updateRelease(@RequestBody SongEntity song){
        songService.updateById(song);
        return Result.ok();
    }

    /**
     * 批量删除
     *
     * @param ids 歌曲ids
     * @return 统一响应体
     */
    @SysLog(module = Module.MUSIC, operationType = OperationType.DELETE, description = "批量删除歌曲")
    @PreAuthorize("hasAuthority('music:delete')")
    @PostMapping("/batchDel")
    public Result batchDel(@RequestBody List<Integer> ids) {
        songService.removeByIds(ids);
        return Result.ok();
    }

    // 上传歌曲信息Excel
    @SysLog(module = Module.MUSIC, operationType = OperationType.UPLOAD, description = "删除歌曲Excel")
    @PreAuthorize("hasAuthority('file:upload')")
    @PostMapping("/batchSaveSong")
    public Result batchSaveSong(@RequestParam(value = "file") MultipartFile file) {
        songService.batchSaveSong(file);
        return Result.ok();
    }

    @PostMapping("/handleSongList")
    public Result handleSongList(@RequestBody HandleSongListReqVo reqVo) {
        songService.handleSongList(reqVo);
        return Result.ok();
    }
}
